package com.example.Windturbine.Controller;

import com.alibaba.excel.EasyExcel;
import com.example.Windturbine.Controller.ViewObject.Excel.PartExcel;
import com.example.Windturbine.Controller.ViewObject.ProductInfoVO;
import com.example.Windturbine.Dao.ProductDOMapper;
import com.example.Windturbine.Dao.ProductInfoDOMapper;
import com.example.Windturbine.DataObject.ProductInfoDO;
import com.example.Windturbine.Error.BusinessException;
import com.example.Windturbine.Error.EmBusinessError;
import com.example.Windturbine.Listener.ExcelListener;
import com.example.Windturbine.Response.CommonResult;
import com.example.Windturbine.Service.PartService;
import com.example.Windturbine.Service.ProductService;
import com.example.Windturbine.SimilarLearning.BP;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.*;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author 丁永新
 * @Date 2021/12/19
 */
@Slf4j
@Controller("part")
@RequestMapping("/part")
@CrossOrigin(allowCredentials = "true", allowedHeaders = "*")
public class PartController {

    @Autowired
    PartService partService;

    @Autowired
    ProductService productService;

    @Resource
    ProductDOMapper productDOMapper;

    @Resource
    ProductInfoDOMapper productInfoDOMapper;

    @Resource
    BP bp;

    @Autowired
    RedisTemplate redisTemplate;



    @RequestMapping("/getall")
    @ResponseBody
    public CommonResult getAllProduct() throws BusinessException, UnsupportedEncodingException, NoSuchAlgorithmException {
        List<ProductInfoDO> all = partService.getAll();
        List<ProductInfoVO> list=new ArrayList<>();
        for(ProductInfoDO i:all){
            list.add(convertFromDO(i));
        }
        return CommonResult.create(list);
    }


    @RequestMapping("/getuserpart")
    @ResponseBody
    public CommonResult getUserProduct(@RequestParam(name = "id") Integer user_id) throws BusinessException, UnsupportedEncodingException, NoSuchAlgorithmException {
        List<ProductInfoDO> userPart = partService.getUserPart(user_id);
        List<ProductInfoVO> list=new ArrayList<>();
        for(ProductInfoDO i:userPart){
            list.add(convertFromDO(i));
        }
        return CommonResult.create(list);
    }



    @RequestMapping(value = "/submitfile", method = {RequestMethod.POST}, consumes = {"application/x-www-form-urlencoded", "multipart/form-data"})
    @ResponseBody
    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, rollbackFor = Exception.class)
    public CommonResult submitProduct(@RequestParam(name = "userVOIdFile") Integer userVOIdFile,@RequestParam(name = "uploadfile") MultipartFile file) throws BusinessException, IOException, NoSuchAlgorithmException {
        if (file == null) {
            log.error("submitProduct：上传文件失败！");
            throw new BusinessException(new BusinessException(EmBusinessError.UNKNOWN_ERROR, "上传文件失败！"));
        }

        InputStream in = file.getInputStream();
        //EasyExcel.read(in, PartExcel.class, new ExcelListener(productService, 0, userVOIdFile,productInfoDOMapper,productDOMapper)).sheet().doRead();
        EasyExcel.read(in, PartExcel.class, new ExcelListener(productService, 0, userVOIdFile,productInfoDOMapper,productDOMapper,bp,redisTemplate)).sheet().doRead();
        return CommonResult.create(null);
    }


    @Transactional
    @RequestMapping(value = "/submit", method = {RequestMethod.POST}, consumes = {"application/x-www-form-urlencoded"})
    @ResponseBody
    public CommonResult submitProduct(@RequestParam(name = "par_order") String par_order,
                                      @RequestParam(name = "par_codename") String par_codename,
                                      @RequestParam(name = "par_name") String par_name,
                                      @RequestParam(name = "par_serial") String par_serial,
                                      @RequestParam(name = "par_function", required = false, defaultValue = "未知") String par_function,
                                      @RequestParam(name = "par_parameter") String par_parameter,
                                      @RequestParam(name = "par_material") String par_material,
                                      @RequestParam(name = "par_weightevery", required = false, defaultValue = "1") Integer par_weightevery,
                                      @RequestParam(name = "par_comments") String par_comments,
                                      @RequestParam(name = "par_origin", required = false, defaultValue = "0") String par_origin,
                                      @RequestParam(name = "par_distance",required = false, defaultValue = "0") Integer par_distance,
                                      @RequestParam(name = "par_transport") String par_transport,
                                      @RequestParam(name = "par_manufacture") String par_manufacture,
                                      @RequestParam(name = "par_recycle") Integer par_recycle,
                                      @RequestParam(name = "par_productid") Integer par_productid,
                                      @RequestParam(name = "par_userid") Integer par_userid ) throws BusinessException, IOException, NoSuchAlgorithmException {

        ProductInfoDO productInfoDO=new ProductInfoDO();
        productInfoDO.setOrderInfo(par_order);
        productInfoDO.setCodenameInfo(par_codename);
        productInfoDO.setNameInfo(par_name);
        productInfoDO.setSerialInfo(par_serial);
        productInfoDO.setFunctionInfo(par_function);
        productInfoDO.setParameterInfo(par_parameter);
        productInfoDO.setMaterialInfo(par_material);
        productInfoDO.setWeighteveryInfo(par_weightevery);
        productInfoDO.setCommentsInfo(par_comments);
        productInfoDO.setOriginInfo(par_origin);
        productInfoDO.setDistanceInfo(par_distance);
        productInfoDO.setTransportstyleInfo(par_transport);
        productInfoDO.setManufacturestyleInfo(par_manufacture);
        productInfoDO.setRecycleInfo(par_recycle);
        productInfoDO.setProductId(par_productid);
        productInfoDO.setUserId(par_userid);
        //先存入产品信息表
        partService.submitPart(productInfoDO);
        return CommonResult.create(null);
    }

    private ProductInfoVO convertFromDO(ProductInfoDO productInfoDO){
        ProductInfoVO productInfoVO=new ProductInfoVO();
        BeanUtils.copyProperties(productInfoDO,productInfoVO);
        productInfoVO.setProductname(productDOMapper.selectByPrimaryKey(productInfoDO.getProductId()).getName());
        return productInfoVO;
    }
}
